<?php

namespace App\Http\Controllers\ChinaBank\Admin;

use App\Models\ChinaBank\GoodsModel;
use App\Models\ChinaBank\OrderGoodsModel;
use App\Models\ChinaBank\OrderModel;
use App\Models\ChinaBank\ShopAddressModel;
use App\Models\ChinaBank\ShopModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class OrderController extends Controller
{
    public function orders(Request $request){
        $brand  = $request->input('brand','');
        $status = $request->input('status',-1);
        $name   = $request->input('name','');
        $date   = $request->input('date','');
        $export = $request->input('export',false);
        $params = [
            "brand" => $brand,
            "status"=> $status,
            "name"  => $name,
            "date"  => $date
        ];

        $shops = $brand ? ShopAddressModel::where('shop',$brand)->pluck('id') : [];
        $orderModel = OrderModel::with('wxuser')->with("shop")->with('card');

        if(count($shops) > 0){
            $orderModel->whereIn('book_shop',$shops);
        }

        if($status >= 0){
            $orderModel->where('status',$status);
        }
        if($name){
            $name = "%".$name."%";
            $orderModel->where(function($db) use ($name){
                $db->where('user_name','like',$name)->orWhere('user_mobile','like',$name);
            });
        }

        if($date){
            $orderModel->where('book_date','like',$date."%");
        }

        $orderModel->orderBy('order_id','desc')->whereNull('deleted_at');

        if($export){
            $orders = $orderModel->get();
        }else{
            $orders = $orderModel->paginate(15);
        }

        $status = OrderModel::$status;
        foreach($orders as $order){
            $goods  = OrderGoodsModel::where('order_id',$order->order_id)->pluck('goods_id');
            $goods  = GoodsModel::whereIn('id',$goods)->get();
            $order->goods = $goods;
            $order->status = $status[$order->status];
        }

        //导出内容
        if($export){
            $exportData = [["用户名","手机","门店","时间","产品","卡号","卡密","供应商卡号","供应商卡密","状态","留言"]];
            foreach($orders as $o){
                $exportData[] = [
                    $o->user_name,
                    $o->user_mobile,
                    $o->shop->name,
                    $o->book_date,
                    $o->goods[0]->name,
                    $o->card->account,
                    $o->card->password,
                    $o->shop_card,
                    $o->shop_pwd,
                    $o->status,
                    $o->note
                ];
            }
            $export = new OrderExport($exportData);
            return Excel::download($export, "深圳中行积分订单".date('YmdHis').'.xlsx');
        }

        $brands = ShopModel::get();
        $data = [
            'orders'=>$orders,
            'status'=>$status,
            'brands'=>$brands,
            'params'=>$params
        ];
        return self::adminview("order/order-list",$data);
    }


    public function orderDel(Request $request)
    {
        $id = $request->input('id');
        OrderModel::where('order_id',$id)->delete();
        return self::send();
    }

    public function orderEditView(Request $request)
    {
        $order  = OrderModel::with('shop')->find($request->input('id'));

        $shops  = ShopAddressModel::where('shop',$order->shop->shop)->get();

        $status = OrderModel::$status2;

        return self::adminview("order/order-edit",['order'=>$order,'shops'=>$shops,"status"=>$status]);
    }

    public function orderEdit(Request $request){

        $data = $request->except("_token");
        OrderModel::where('order_id',$data['order_id'])->update($data);
        return self::send();
    }


}
